package com.zhejiangzhengyuan.municipal_common.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhejiangzhengyuan.municipal_common.entity.Role;
import com.zhejiangzhengyuan.municipal_common.entity.Subject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhejiangzhengyuan.municipal_common.entity.vo.SubjectVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 权限模块中的认证主体表，与业务系统中的用户数据一一对应，存储用户的用户名、密码、权限关系起点。单独存储是为了将权限和业务解耦 Mapper 接口
 * </p>
 *
 * @author linju
 * @since 2019-11-22
 */
@Repository
public interface SubjectMapper extends BaseMapper<Subject> {


    @Select("select * from subject  where subject_name = #{name}")
    Subject findByName(@Param("name") String name);

    @Select("select r.* from subject s,role r,subject_role_mapping srm\n" +
            "where s.id=srm.subject_id and r.id=srm.role_id")
    List<Role> selectSubjectByRole();

    @Select("select * from user_info u, subject s, subject_user_info_mapping sum\n" +
            "              where s.id=sum.subject_id and u.id= sum.user_info_id and s.enabled='t'")
    Page<SubjectVo> selectAllUserInfo(Page<SubjectVo> subjectVoPage);


    @Select("select s.* from user_info u, subject s, subject_user_info_mapping sum\n" +
            "            where s.id=sum.subject_id and u.id= sum.user_info_id and u.id=#{uid}")
    Subject selectSubjectByUserInfoId(@Param("uid") Long id);

    @Select("select * from user_info u, subject s, subject_user_info_mapping sum\n" +
            "     where s.id=sum.subject_id and u.id= sum.user_info_id and subject_id =#{sid}")
    SubjectVo selectSubjectVoBySubjectId(@Param("sid") Long sid);
    @Select("select srm.subject_id from \"role\" r left join subject_role_mapping srm on r.id=srm.role_id where role_id =#{rid}")
    List<Long> selectSubjectIdByRoleId(@Param("rid") Long rid);

    @Select("select * from user_info u, subject s, subject_user_info_mapping sum\n" +
            "    where s.id=sum.subject_id and u.id= sum.user_info_id and s.enabled='t' and s.id =#{sid}")
    SubjectVo selectSubjectById(@Param("sid") Long id);

    @Select("select srm.subject_id from \"role\" r left join subject_role_mapping srm on r.id=srm.role_id where role_name like concat(concat('%',#{rName}),'%')")
    List<Long> selectSubjectIdByRoleName(@Param("rName") String rName);

    Page<SubjectVo> selectAllFuzzy(Page<SubjectVo> page,@Param("subjectName")String subject,@Param("rName") String role);
}
